package campeonatofutebol.util;

import java.util.ArrayList;
import java.util.Collection;

import android.app.Dialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import campeonatofutebol.activity.ClassificacaoList;

import com.example.campeonatofutebol.R;

public class CarregandoFeedsTask extends AsyncTask<Object, Object, Collection<String>>{

	ListActivity listActivityAtual = null;
	private ClassificacaoList activity = null;
	boolean cancelado = false;
	Dialog progresso;
	Intent i = null;
	private String timeAtual;
	private String rssAtual;
	private String campeonatoAtual;
	private Collection<String> feeds = null;
	private String diaDaSemana;
	Collection<String> jogosDaSemana = new ArrayList<String>();

	public CarregandoFeedsTask(ListActivity activity) {
		this.listActivityAtual = activity;

	}
	
	public CarregandoFeedsTask(ClassificacaoList act){
		this.activity = act;
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		Context ctx;
		if(listActivityAtual != null){
			ctx = listActivityAtual;
		}else{
			ctx = activity;
		}
		progresso = ProgressDialog.show(ctx, 
				"Carregando", "Por favor, espere...");

	}

	@Override
	protected Collection<String> doInBackground(Object... params) {

		try {
			rssAtual = CarregadorDeFeeds.getInstance().getRssAtual();
			timeAtual = CarregadorDeFeeds.getInstance().getTimeAtual();
			campeonatoAtual = CarregadorDeFeeds.getInstance().getCampeonatoAtual();
			feeds = CarregadorDeFeeds.getInstance().carregaFeed();

		} catch (Exception e) {
			feeds = new ArrayList<String>();
			feeds.add("Erro na interpreta��o do arquivo");
			e.printStackTrace();
		}
		if(diaDaSemana != null){
			for (String msg : feeds) {
				if(msg.toLowerCase().contains(diaDaSemana.toLowerCase())){
					jogosDaSemana.add(msg);
				}
			}
			return jogosDaSemana;
		}

		return feeds;
	}

	@Override
	protected void onPostExecute(Collection<String> result) {
		ArrayAdapter<String> adapter = null;
		if(result.isEmpty()){
			Toast.makeText(listActivityAtual.getApplicationContext(), "Nenhuma informação obtida, recarregue a página!",
    			    Toast.LENGTH_LONG).show();
		}
		if((rssAtual != null) || (timeAtual != null) || (campeonatoAtual != null)){
			adapter = new ArrayAdapter<String>(listActivityAtual.getBaseContext(), R.layout.row_withimage,new ArrayList<String>(result));
		}
		else{
			if(activity!= null){
				activity.formatarTabela();
			}else if (listActivityAtual != null){
				adapter = new ArrayAdapter<String>(listActivityAtual.getBaseContext(), R.layout.row,new ArrayList<String>(result));
			}
		}
		if(activity == null){
			listActivityAtual.setListAdapter(adapter);
		}
		if(progresso.isShowing()){
			progresso.dismiss();

		}
	}

	
	@Override
	protected void onCancelled() {
		cancelado = true;
		super.onCancelled();
	}

	public Collection<String> getResult(){
		return feeds;
	}

	public String getDiaDaSemana() {
		return diaDaSemana;
	}

	public void setDiaDaSemana(String diaDaSemana) {
		this.diaDaSemana = diaDaSemana;
	}
	
	public Collection<String> getJogosDaSemana() {
		return jogosDaSemana;
	}


}
